package com.springboot.controller;

import com.springboot.constant.CommonConstant;
import com.springboot.domain.BtBank;
import com.springboot.domain.BtDemand;
import com.springboot.domain.Result;
import com.springboot.dto.*;
import com.springboot.enums.ResultEnum;
import com.springboot.jpa.BankJPA;
import com.springboot.repository.BankRepository;
import com.springboot.repository.DemandRepository;
import com.springboot.repository.TaskRepository;
import com.springboot.service.BankService;
import com.springboot.tools.ResultUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;


@RestController
public class BankController {

    private BankService bankService;
    private BankRepository bankRepository;
    private DemandRepository demandRepository;
    private TaskRepository taskRepository;
    private BankJPA bankJPA;

    @Autowired
    public BankController(BankService bankService, BankRepository bankRepository, DemandRepository demandRepository, TaskRepository taskRepository,BankJPA bankJPA) {
        this.bankService = bankService;
        this.bankRepository = bankRepository;
        this.demandRepository = demandRepository;
        this.taskRepository = taskRepository;
        this.bankJPA=bankJPA;
    }

    //银行信息详情
    @ResponseBody
    @GetMapping(value = "/bank/bankInfoDetail")
    public Result bankDetail(HttpSession session) {
        String id = (String) session.getAttribute("user");
        if (null == id) {
            return ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
        return bankService.findBankDetails(id);
    }

    //银行信息详情 (其他人查看)
    @ResponseBody
    @GetMapping(value = "/bank/findBankDetailById/{id}")
    public Result findBankDetailById(@PathVariable String id) {
        return bankService.findDetailById(id);
    }

    @PostMapping(value = "/bank/modify")
    public Result modify(@Valid @RequestBody BankModifyDTO bankModifyDTO, BindingResult bindingResult, HttpSession session) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            for (ObjectError error : errorList) {
                return ResultUtil.error(error.getDefaultMessage());
            }
        }
        bankService.modify(bankModifyDTO, session);
        return ResultUtil.success();
    }

    @ResponseBody
    @GetMapping(value = "/bank/logout/list")
    public Result logoutList(@ApiParam Pageable pageable) {
        Page<BtBank> pages = bankRepository.findByStatus(CommonConstant.LOGOUTING, pageable);
        return ResultUtil.success(pages);
    }

    //新增需求
    @ResponseBody
    @PostMapping(value = "/bank/demandSubApply")
    public Result addDemand(@Valid @RequestBody DemandSubApplyDTO demandSubApplyDTO, BindingResult bindingResult, HttpSession session) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            for (ObjectError error : errorList) {
                return ResultUtil.error(error.getDefaultMessage());
            }
        }
        bankService.addDemand(demandSubApplyDTO, session);
        return ResultUtil.success("添加需求成功");
    }

    //银行需求列表
    @ResponseBody
    @GetMapping(value = "/bank/demandSubList")
    public Result demandSubList(@ApiParam Pageable pageable, HttpSession session) {
        String id = session.getAttribute("user").toString();
        Page<BtDemand> btDemandSub = demandRepository.findDemandByBankId(id, pageable);
        return ResultUtil.success(btDemandSub);
    }

    //银行需求详情   demand表
    @ResponseBody
    @GetMapping(value = "/bank/demandSubDetail/{id}")
    public Result demandSubDetail(@PathVariable String id) {
        DemandDetailUserDTO demandDetailUserDTO = bankService.demandDetail(id);
        return ResultUtil.success(demandDetailUserDTO);
    }

    //银行已签约列表
    @ResponseBody
    @GetMapping(value = "/bank/demand/banksigned/list")
    public Result signedDemandList(@ApiParam Pageable pageable, HttpSession session) {
        String userId = (String) session.getAttribute("user");
        if (null == userId) {
            ResultUtil.error(ResultEnum.NOT_LOGIN);
        }
        Page<BtDemand> btDemandSub = demandRepository.findSignedListByBank(userId, pageable);
        return ResultUtil.success(btDemandSub);
    }

    //银行已签约项目详情
    @ResponseBody
    @GetMapping(value = "/bank/demandSignedDetail/{id}")
    public Result demandSignedDetail(@PathVariable String id) {
        DemandSignedDTO demandSignedDetail = bankService.demandSignedDetail(id);
        return ResultUtil.success(demandSignedDetail);
    }

    //银行需求修改
    @ResponseBody
    @PostMapping(value = "/bank/demandModify")
    public Result demand(@Valid @RequestBody DemandModifyDTO demandModifyDTO, BindingResult bindingResult, HttpSession session) {
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            for (ObjectError error : errorList) {
                return ResultUtil.error(error.getDefaultMessage());
            }
        }
        bankService.demandModify(demandModifyDTO, session);
        return ResultUtil.success("需求修改成功");
    }

    //task list
    @ResponseBody
    @GetMapping(value = "/bank/task/taskList")
    public Result taskList(@ApiParam Pageable pageable, HttpSession session) {
        String bankId = (String) session.getAttribute("user");
        Page<TaskDemandDTO> tasks = taskRepository.findByAnnouncer(bankId, pageable);
        return ResultUtil.success(tasks);
    }

    @ResponseBody
    @GetMapping(value = "/bank/modified/list")
    public Result modifiedList(@ApiParam Pageable pageable) {
        return ResultUtil.success(bankJPA.getBankList(CommonConstant.PASS, pageable));
    }

    /*银行的成功对接记录*/
    @ResponseBody
    @GetMapping(value = "/bank/successRecord/{bankId}")
    public Result<List<SuRecordDTO>> bankSuRecord(@PathVariable String bankId) {
        Result<List<SuRecordDTO>> result = bankService.findBankSuRecord(bankId);
        return result;
    }
}
